package com.list88zhu.android.sunflower.jetpack.room;

import android.arch.persistence.db.SupportSQLiteDatabase;
import android.arch.persistence.room.Database;
import android.arch.persistence.room.Room;
import android.arch.persistence.room.RoomDatabase;
import android.arch.persistence.room.TypeConverters;
import android.arch.persistence.room.migration.Migration;
import android.content.Context;
import android.support.annotation.NonNull;

/**
 * <pre><code>
 *
 *  </code></pre>
 *
 * @author zhuyingming@qmtv.com
 * @date 2018/8/26
 */
@Database(entities = {Plant.class}, version = 2, exportSchema = false)
@TypeConverters(ConversionFactory.class)
public abstract class AppDatabase extends RoomDatabase {

    public abstract PlantDao getPlantDao();

    public static AppDatabase getDefault(Context context) {
        return buildAppDatabase(context.getApplicationContext());
    }

    private static AppDatabase buildAppDatabase(Context context) {
        return Room.databaseBuilder(context, AppDatabase.class, "sunflower.db")
            .allowMainThreadQueries()
            .addMigrations(MIGRATION_1_2)
            .build();
    }


    static final Migration MIGRATION_1_2 = new Migration(1, 2) {
        @Override
        public void migrate(@NonNull SupportSQLiteDatabase database) {
            database.execSQL("ALTER TABLE PLANT ADD COLUMN PROGRESS INTEGER NOT NULL DEFAULT 0");
        }
    };

}
